package candidateView;

import electionCtrl.Candidate;
import electionCtrl.Admin;
import electionView.ElectionGui;
import electionView.PasswordGui;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Collection;
import java.util.LinkedList;
import java.util.concurrent.Semaphore;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;

//we need to change governor to "governor / Lt. governor 
public class CandidateList extends ElectionGui{

	/**
	 * This instance field is used in order to create a text area field.
	 */
	private final JTextField candidateNameTextArea;
	/**
	 * This instance field is used in order to create a text area field.
	 */
	private final JTextField candidatePartyTextArea;
	/**
	 * This instance field is used in order to create the submit button.
	 */
	private final JButton newCandidateButton;
	/**
	 * This instance field is used in order to create the done button.
	 */
	private final JButton doneButton;
	/**
	 * This instance field is used in order to create the panel to add the Radio buttons.
	 */
	private final JPanel panelRadio;
	/**
	 * This instance field is used in order to hold instanciated RadioButtons.
	 */
	private final JRadioButton[] button;
	/**
	 * This instance field is used in order to group all the Radio buttons together.
	 */
	private final ButtonGroup radioGroup;
	/**
	 * This instance field is used in order to create the temporary ArrayList to hold the candidates.
	 */
	private final Collection<Candidate> cand;
	/**
	 * This instance field is used in order assign the candidates a seat.
	 */
	private Candidate.Seat seat;
	private final PasswordGui passwordGui;

	public CandidateList()
	{
		super(null, new JFrame("Input the Candidates"), new Semaphore(0));
		this.candidateNameTextArea = new JTextField("Enter Candidate Name Here",19);
		this.candidatePartyTextArea = new JTextField("Enter Candidate Party Here",19);
		this.newCandidateButton = new JButton("Next Candidate");
		this.doneButton = new JButton("Done");
		this.panelRadio = new JPanel(new GridLayout(6,0));
		this.button = new JRadioButton[Candidate.Seat.values().length];
		this.radioGroup = new ButtonGroup();
		this.cand = new LinkedList<Candidate>();
		this.passwordGui = new PasswordGui(null,this.semaphore);
		
		int i = 0;
		for(Candidate.Seat s : Candidate.Seat.values())
		{
			this.button[i] = new JRadioButton(s.toString());
			i++;
		}
		
		this.newCandidateButton.addActionListener(new NewCandidateButtonListener());
		this.doneButton.addActionListener(new DoneButtonListener());
		
		for(JRadioButton button:this.button)
		{
			button.addActionListener(new CandidatesRadioButtonListener());
			this.radioGroup.add(button);
			this.panelRadio.add(button);
		}
		
		this.mainFrame.setLayout(new FlowLayout());
		this.mainFrame.add(this.candidatePartyTextArea, BorderLayout.NORTH);
		this.mainFrame.add(this.candidateNameTextArea, BorderLayout.NORTH);
		this.mainFrame.add(this.panelRadio,BorderLayout.CENTER);
		this.mainFrame.add(this.newCandidateButton, BorderLayout.SOUTH);
		this.mainFrame.add(this.doneButton, BorderLayout.SOUTH);
		this.mainFrame.setSize(230,125 + (this.button.length > 0 ? 50 : 0) + (this.button.length > 1 ? 25*(this.button.length-1) : 0));
	}

	/**
	 * This method is used inorder to call the createGui. The while loop causes the program to pause until the 
	 * Done button is pressed. After the Done buttons is pressed then it creates the Candidate array equal to the 
	 * ArrayList of Candidate. THen if the for loop it adds the Candidates from the ArrayList into the Candidate array.
	 *@return This returns the array of Candidates that is needed throught the whole program.
	 */
	protected Candidate[] getList() throws InterruptedException
	{
		this.semaphore.acquire();
		return this.cand.toArray(new Candidate[0]);
	}
	
	public Admin getAdmin() throws InterruptedException
	{
		JOptionPane.showMessageDialog(null, "Please input a username and password to create administrator credentials");
		String [] userPass = new String [2];
		while(userPass[0] == null || userPass[0] == "" || userPass[1] == null || userPass[1] == "") {
			this.passwordGui.showWindow();
			this.passwordGui.enable();
			PasswordGui.Button pgbutton = passwordGui.waitForAction();
			if(pgbutton == PasswordGui.Button.CANCEL)
			{
				JOptionPane.showMessageDialog(null, "You must input a username and password for the first admin.");
				continue;
			}
			this.passwordGui.disable();
			this.passwordGui.hideWindow();
			userPass = this.passwordGui.getUsernameAndPassword();
		}
		return new Admin(userPass);
	}

	private class CandidatesRadioButtonListener implements ActionListener
	{
		/**
		 * This method is used in order to give the buttons functionality. When a button is clicked it matches the string
		 * of the button clicked to the string. If there is a match then the statements inside the if statements run.
		 * @param e This is the ActionEvent needed in order to make the program actions work.
		 */
		public void actionPerformed(ActionEvent e)
		{
			for(Candidate.Seat seatCheck : Candidate.Seat.values())
			{
				if(e.getActionCommand().equals(seatCheck.toString()))
				{
					seat = seatCheck;
					break;
				}
			}
		}
	}

	private class NewCandidateButtonListener implements ActionListener
	{
		@Override
		public void actionPerformed(ActionEvent e) 
		{
			try
			{
				Candidate can = new Candidate(candidateNameTextArea.getText(), seat, candidatePartyTextArea.getText());
				cand.add(can);
			}
			catch(IllegalArgumentException exception)
			{
				JOptionPane.showMessageDialog(null, "Please click one of the Radio Buttons!", "Error", JOptionPane.ERROR_MESSAGE);
				return;
			}
			try
			{
				JOptionPane.showMessageDialog(null, "Candidate: " + candidateNameTextArea.getText() + "\n Seat: " + seat.toString() + "\n Party: "+ candidatePartyTextArea.getText() + "\n Was added Successfully.");
			}
			catch(NullPointerException exeception)
			{
				JOptionPane.showMessageDialog(null, "Candidate failed to be added!", "Error", JOptionPane.ERROR_MESSAGE);
				return;
			}
			candidateNameTextArea.setText(null);
			//fieldArea2.setText(null);
		}
	}

	private class DoneButtonListener implements ActionListener
	{
		@Override
		public void actionPerformed(ActionEvent e) 
		{
			if(cand.size() !=0)
			{
				mainFrame.setVisible(false);
				semaphore.release();
			}
			else
			{
				JOptionPane.showMessageDialog(null, "Please add some candidates! Preferrably one in each category, thank you!", "Error", JOptionPane.ERROR_MESSAGE);
			}
		}
	}
}
